﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.BussLogic;
using TimKen.App;
using TimKen.DataEntity;

namespace TimKen.Web.Admin.Brand
{
    public partial class Brand_Add : AdminPage
    {
        private PBrandManager ttm = new PBrandManager();

        public int BrandID
        {
            get
            {
                if (ViewState["BrandID"] != null)
                    return Convert.ToInt32(ViewState["BrandID"]);
                else
                    return 0;
            }
            set
            {
                ViewState["BrandID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindResources();

                if (Request.QueryString["BrandID"] != null)
                {
                    BrandID = Convert.ToInt32(Request.QueryString["BrandID"]);
                }
                if (BrandID != 0)
                    BindData();
            }
        }
        private void BindData()
        {
            DataEntity.PBrand brand = ttm.GetModel(BrandID);
            txtBrandName.Text = brand.Name;
            txtUrl.Text = brand.Url;
            fckDescription.Value = brand.Description;
            this.txtLogo.Text = brand.Logo;

            if (!string.IsNullOrEmpty(brand.Logo))
                this.imgLogoImage.ImageUrl = UploadFilePathForProduct + brand.Logo;
            else
                this.imgLogoImage.Visible = false;
        }

        private void BindResources()
        {
            this.btnSave.Text = GetLangString("Save");

        }
        protected void ResetForm()
        {
            txtBrandName.Text = "";

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            TimKen.DataEntity.PBrand brand = new PBrand();
            brand.Name = txtBrandName.Text;

            if (BrandID != 0)
            {
                brand.BrandID = BrandID;
                brand.Name = this.txtBrandName.Text;
                brand.Url = this.txtUrl.Text;
                brand.Logo = this.txtLogo.Text;
                brand.Description = this.fckDescription.Value;
                brand.CompanyName = this.txtCompanyName.Text;
                ttm.Update(brand);
            }
            else
            {
                brand.BrandID = ttm.GetMaxId() + 1;
                brand.Name = this.txtBrandName.Text;
                brand.Url = this.txtUrl.Text;
                brand.Logo = this.txtLogo.Text;
                brand.Description = this.fckDescription.Value;
                brand.CompanyName = this.txtCompanyName.Text;
                ttm.Add(brand);
            }
            Response.Redirect("Brand_List.aspx");
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.txtLogo.Text != "")
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(PhysicalUploadFilePathForProduct + this.txtLogo.Text);
                if (fileInfo.Exists)
                {
                    try
                    {
                        fileInfo.Delete();

                        this.imgLogoImage.Visible = false;
                        this.imgLogoImage.ImageUrl = "";
                        this.txtLogo.Text = "";
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("删除失败,错误信息" + ex.Message);
                    }
                }

            }
        }


        protected void btnRefresh_Click(object sender, EventArgs e)
        {

            this.imgLogoImage.Visible = true;
            this.imgLogoImage.ImageUrl = UploadFilePathForProduct + this.txtLogo.Text;



        }



    }
}
